package de.akabahn.ebd.control;

import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
import de.akabahn.ebd.control.auth.AccessEnum;
import de.akabahn.ebd.control.auth.UserAuthObject;

/**
 * @author Peter Eimann
 * @since 0.1
 * @version 0.2
 */
public class EBD_UnregisteredLoginActivity extends EBD_DefaultUncaughtExceptionHandler {

	private final static UserAuthObject GUEST_USER = new UserAuthObject("Gast", null, AccessEnum.RESTRICTED, null, null);

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.unregisteredlogin);
		PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

		handleLoginButton();
	}

	private void handleLoginButton() {
		Button loginButton = (Button) findViewById(R.id.unregisteredlogin_button_login);
		loginButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String bahnhof = ((Spinner)findViewById(R.id.unregisteredlogin_spinner_bahnhof)).getSelectedItem().toString();

				if (bahnhof.startsWith("--")) {
					Toast.makeText(EBD_UnregisteredLoginActivity.this,
							getResources().getString(R.string.login_noBahnhof), Toast.LENGTH_LONG).show();
					return;
				}

				Intent next = new Intent(EBD_UnregisteredLoginActivity.this, EBD_HandreglerActivity.class);
				next.putExtra("de.akabahn.ebd.control.userName", GUEST_USER.getUsername());
				next.putExtra("de.akabahn.ebd.control.roleName", GUEST_USER.getAccessRights().name());
				next.putExtra("de.akabahn.ebd.control.bhf", bahnhof.substring(bahnhof.indexOf('(')));
				startActivity(next);
			}
		});
	}
}